<template>
  <div class="edituserinfo-container">
    <el-form :label-position="labelPosition" label-width="80px" :model="userInfo">
      <el-form-item label="ID">
        <el-input v-model="userInfo._id" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="userInfo.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="userInfo.password"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="userInfo.email"></el-input>
      </el-form-item>
      <el-form-item label="角色">
        <el-input v-model="userInfo.role"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="editUserInfo(userInfo)">提交</el-button>
        <el-button @click="getback">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import eventBus from '@/plugins/eventBus.js'

export default {
  name: 'EditUserInfo',
  data () {
    return {
      labelPosition: 'right',
      userInfo: {
        _id: '',
        username: '',
        password: '',
        email: '',
        role: ''
      }
    }
  },
  created () {
    eventBus.$on('share', this.getHandleInfo)
  },
  methods: {
    async editUserInfo (formData) {
      console.log(formData)
      const res = await this.$api.editUserInfo(formData)
      console.log(res)
      if (res.data.err_code === 0) {
        this.$notify({
          title: '成功',
          message: res.data.message,
          type: 'success'
        })
        this.$router.push('/system/home/user')
      } else {
        this.$notify.error({
          title: '失败',
          message: '修改失败，请检查服务器。错误提示：' + res.data.message
        })
        console.log(res.data.message)
      }
    },
    getback () {
      this.$router.replace('/system/home/user')
    },
    getHandleInfo (val) {
      // console.log(val)
      this.userInfo = {
        _id: val.info._id,
        username: val.info.username,
        password: val.info.password,
        email: val.info.email,
        role: val.info.role
      }
      // console.log(this.userInfo)
    }
  },
  beforeDestroy () {
    eventBus.$off('share', this.getHandleInfo)
  }
}
</script>

<style lang="less" scoped></style>
